# Author:Zhang Yu
from socketserver import BaseRequestHandler,TCPServer, ThreadingTCPServer
from threading import Thread
class EchoHandler(BaseRequestHandler):
    def handle(self):
        print("Got connection from ",self.client_address)
        while True:
            try:
                msg = self.request.recv(1024).strip()
            except ConnectionResetError as err:
                print("close connection",self.client_address)
                break
            # print("{},work".format(self.client_address[0]))
            if not msg:
                self.request.send("Null")
                break
            self.request.send(msg)


if __name__ == '__main__':
    #serv = TCPServer(('',20000),EchoHandler)
    NWORKERS = 2

    serv = ThreadingTCPServer(('', 20000), EchoHandler) #多线程
    for n in range(NWORKERS):
        t = Thread(target=serv.serve_forever())
        t.daemon = True
        t.start()
    serv.serve_forever()
